package com.wanczy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanczy.pojo.MenuTable;
import com.wanczy.pojo.UserTable;
import com.wanczy.service.MenuTableService;
import com.wanczy.service.impl.MenuTableServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@WebServlet("/FindDownMenuServlet")
public class FindDownMenuServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		String menuLevel = request.getParameter("menuLevel");
		MenuTableService service = new MenuTableServiceImpl();
		UserTable user = (UserTable) session.getAttribute("userTable");
		List<MenuTable> list = service.findDownMenu(menuLevel, user.getUser_id());
		JSONArray jsonArray = new JSONArray();
		Iterator<MenuTable> it = list.iterator();
		while(it.hasNext()) {
			MenuTable menu = it.next();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("menuId", menu.getMenu_id());
			jsonObject.put("menuName", menu.getMenu_name());
			jsonObject.put("menuDescribe", menu.getMenu_describe());
			jsonObject.put("menuState", menu.getMenu_state());
			jsonObject.put("menuUrl", menu.getMenu_url());
			jsonObject.put("menuLevel", menu.getMenu_level());
			jsonArray.add(jsonObject);
		}
		out.println(jsonArray.toString());
		out.close();
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
